//
//  MainTabBarViewController.m
//  EducationOID
//
//  Created by slz on 2017/5/9.
//  Copyright © 2017年 bnsr. All rights reserved.
//

#import "MainTabBarViewController.h"
#import "TTNavigationController.h"
#import "HomeViewController.h"
#import "MineViewController.h"
#import "WorkViewController.h"
#import "StudyViewController.h"
#import "AppDelegate.h"
#import "TTLoginViewController.h"

#import "PlayVideoViewController.h"

#import "MFWSDKNotificationConsts.h"

@interface MainTabBarViewController ()<UITabBarControllerDelegate,UINavigationControllerDelegate>

@end

@implementation MainTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNavigationController];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userDidLogout) name:kSDK_Notify_UserDidLogout object:nil];
    // Do any additional setup after loading the view.
}

- (void)userDidLogout{
    [self presentLoginViewController];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self presentLoginViewController];
}

- (void)presentLoginViewController
{
    if (![MFWSdkEnvironment sharedInstance].hasLogin){
        TTLoginViewController * loginController = [[TTLoginViewController alloc]init];
        TTNavigationController * navController = [[TTNavigationController alloc]initWithRootViewController:loginController];
        [[AppDelegate sharedInstance].mainPageController presentViewController:navController animated:YES completion:NULL];
    }
}

-(instancetype)init
{
    self = [super init];
    if(self){
        self.delegate = self;
    }
    return self;
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
//    if (tabBarController.selectedIndex == 2){
//        if (![TTSdkEnviroment sharedInstance].hasLogin){
//            [MainTabBarViewController presentLoginViewControllerFromTabController];
//        }
//    }
}


//
//+ (void)presentLoginViewControllerFromTabController{
//    OILoginViewController * loginController = [[OILoginViewController alloc]init];
//    OINavigationController * navController = [[OINavigationController alloc]initWithRootViewController:loginController];
//    @weakify(navController);
//    loginController.dismiss = ^ (BOOL isSuccessed){
//        @strongify(navController);
//        [navController dismissViewControllerAnimated:YES completion:NULL];
//        if (isSuccessed){
//            [AppDelegate sharedInstance].mainPageController.selectedIndex = 2;
//        }
//    };
//    [[AppDelegate sharedInstance].mainPageController presentViewController:navController animated:YES completion:NULL];
//}


- (void)initNavigationController{
    TTNavigationController *nav1 = [self createClass:[HomeViewController class]
                                                name:@"主页"
                                             imgName:@"tabbar_home"
                                       imgSelectName:@"tabbar_home_select"];

    TTNavigationController *nav2 = [self createClass:[StudyViewController class]
                                                name:@"学习"
                                             imgName:@"tabbar_study"
                                       imgSelectName:@"tabbar_study_select"];
    
    TTNavigationController *nav3 = [self createClass:[PlayVideoViewController class]
                                                name:@"动态"
                                             imgName:@"tabbar_work"
                                       imgSelectName:@"tabbar_work_select"];
    
    TTNavigationController *nav4 = [self createClass:[MineViewController class]
                                                name:@"个人"
                                             imgName:@"tabbar_mine"
                                       imgSelectName:@"tabbar_mine_select"];
    
    self.viewControllers = [NSArray arrayWithObjects:
                            nav1,
                            nav2,
                            nav3,
                            nav4,
                            nil];
    //    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"all_bg.png"]]];
//    [[UITabBar appearance] setTintColor:YELLOWCOLOR];
//    [[UITabBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithRGB:0xf8f8f8 alpha:1]]];
//
//    
    [[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blackColor]}
                                             forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor colorWithRGB:0x27c996]}
                                             forState:UIControlStateSelected];
    
    
//    UIImage *image = [UIImage imageWithColor:[UIColor blueColor]];
//    
//    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    
//    [UINavigationBar appearance].backIndicatorImage = image;
//    
//    [UINavigationBar appearance].backIndicatorTransitionMaskImage = image;
    //updatMsgCount
    
    [UINavigationBar appearance].tintColor = kBack_Color_Default;
    
    
    self.selectedIndex = 0;
}

- (TTNavigationController*)createClass:(Class)vcClass
                                  name:(NSString*)name
                               imgName:(NSString*)imgName
                         imgSelectName:(NSString*)selectName
{
    UIViewController *vc = [[vcClass alloc] init];
    vc.tabBarItem = [self createUITabBarItem:name
                                         img:imgName
                                 selectedImg:selectName];
    
    TTNavigationController *nav = [[TTNavigationController alloc] initWithRootViewController:vc];
    return nav;
}

-(UITabBarItem*)createUITabBarItem:(NSString*)title img:(NSString*)imgName selectedImg:(NSString*)selectimg{
    UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:title image:[[UIImage imageNamed:imgName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:selectimg]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    return item;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
